package com.iexbuy.ihg.ui.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.navisdk.util.common.StringUtils;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.AddressObject;
import com.iexbuy.ihg.entity.AddressObjectResponse;
import com.iexbuy.ihg.entity.ShopItemDetailObject;
import com.iexbuy.ihg.entity.XiaoFeiOrderConfirmObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HuanGouOrderConfirmFragment extends BaseFragment {
	
	private static HuanGouOrderConfirmFragment homeFragmentInstent;
	private View rootView;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.productName)
	private TextView productName;
	@ViewInject(R.id.price)
	private TextView price;
	@ViewInject(R.id.buyNumber)
	private EditText buyNumber;
	@ViewInject(R.id.contanctAddress)
	private TextView contanctAddress;
	@ViewInject(R.id.totalPayAmount)
	private TextView totalPayAmount;
	@ViewInject(R.id.addressInfoLayout)
	private LinearLayout addressInfoLayout;
	@ViewInject(R.id.addressContacter)
	private TextView addressContacter;
	@ViewInject(R.id.addressMobile)
	private TextView addressMobile;
	@ViewInject(R.id.address)
	private TextView address;

	private static ShopItemDetailObject shopItemDetailObject;
	private XiaoFeiOrderConfirmObject xiaoFeiOrderConfirmObject;

	private List<AddressObject> addressObjects = new ArrayList<AddressObject>();
	private AddressObject selectAddress;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_huan_gou_order_confirm, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_huan_gou_order_confirm));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		initOrderlView();
		initDefaultAddress();
	}

	private void initOrderlView() {
		if (shopItemDetailObject == null) return;
		productName.setText(shopItemDetailObject.getItemName());
		price.setText(shopItemDetailObject.getNewPrice());
		totalPayAmount.setText(shopItemDetailObject.getNewPrice());
	}

	private void initDefaultAddress() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_MY_ADDRESS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						AddressObjectResponse rep = JsonUtils.parseJsonStrToObject(responseInfo.result, AddressObjectResponse.class);
						if (rep.getBody() != null) {
							for (AddressObject addressObject : rep.getBody()) {
								if ("1".equalsIgnoreCase(addressObject.getDefAddr())) {
									selectAddress = addressObject;
									initAddressView();
									break;
								}
							}
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initAddressView() {
		if (selectAddress != null) {
			addressInfoLayout.setVisibility(View.VISIBLE);
			contanctAddress.setVisibility(View.GONE);

			addressContacter.setText("收件人："+selectAddress.getReceiver());
			addressMobile.setText("电话："+selectAddress.getMobile());
			address.setText("地址："+selectAddress.getAddress());
		} else {
			addressInfoLayout.setVisibility(View.GONE);
			contanctAddress.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 提交订单
	 * @param view
	 */
	@OnClick(R.id.submitOrderBtn)
	public void onSubmitOrderBtnClick(View view) {
		submitOrder();
	}

	@OnClick(R.id.addressLayout)
	public void addressLayoutClick(View view) {
		WeDeAddresssFragment.setOnSelectedListener(new WeDeAddresssFragment.OnSelectedListener() {
			@Override
			public void onSelected(AddressObject addressObject) {
				selectAddress = addressObject;
				initAddressView();
			}
		});
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeAddresssFragment.class);
	}

	/**
	 * 下订单
	 */
	private void submitOrder() {
		HuanGouOrderPayFragment.setShopItemDetailObject(shopItemDetailObject);
		HuanGouOrderPayFragment.setSelectedAddress(selectAddress);
		CommUtil.openSingleFragmentActivity(mBaseActivity, HuanGouOrderPayFragment.class);
	}


	public static void setShopItemDetailObject(ShopItemDetailObject shopItemDetailObject) {
		HuanGouOrderConfirmFragment.shopItemDetailObject = shopItemDetailObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static HuanGouOrderConfirmFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
